package com.example.thirdhomework

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.service.autofill.SavedDatasetsInfo
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity5 : AppCompatActivity() {

    var seconds = 0
    var running = false
    var wasrunning = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main5)
        Log.d("life cycle","onCreate")
        if(savedInstanceState != null){
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasrunning = savedInstanceState.getBoolean("wasRunning")
        }

        runTimer()

        //开始按钮
        val button1 = findViewById<Button>(R.id.button10)
        button1.setOnClickListener {
            running = true
        }

        //停止按钮
        val button2 = findViewById<Button>(R.id.button11)
        button2.setOnClickListener {
            running = false
        }

        //继续按钮
        val button3 = findViewById<Button>(R.id.button8)
        button3.setOnClickListener {
            running = true
            seconds = 0
        }

        //返回按钮
        val button4 = findViewById<Button>(R.id.button9)
        button4.setOnClickListener {

            startActivity(Intent(this, MainActivity::class.java))
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    @SuppressLint("DefaultLocale")
    fun runTimer() {
        val handler = Handler()
        val runnable = object:Runnable {
            override fun run(){
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60
                val textView = findViewById<TextView>(R.id.textView5)
                textView.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds++
                }
                handler.postDelayed(this, 1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("life cycle","onSaveInstanceState")
        outState.putInt("seconds",seconds)
        outState.putBoolean("running",running)
        outState.putBoolean("wasRunning",wasrunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("life cycle","onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("life cycle","onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle","onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("life cycle","onResume")
        if(wasrunning){
            running = true
        }
    }

    override fun onPause() {
        super.onPause()
        Log.d("life cycle","onPause")
        wasrunning = running
        running = false
    }
}
